<template>
  <div>
    <el-col :span="24" style="padding:10px;padding-left:0">
      <el-button type="primary" @click="addVisible=true"> <i class="el-icon-plus"></i> 新增部门</el-button>
    </el-col>
    <el-table :data="List" border stripe height="600">
      <el-table-column v-for="(v,i) in prop" :key="i" :label="v.label" :prop="v.name" align="center">
        <template scope="scope">
          <span v-if="i==1">
            <span v-if="!scope.row.visible">
              {{scope.row.name}}
            </span>
            <el-input size="small" style="width:200px" v-else v-model="scope.row.name" placeholder=""></el-input>
          </span>
          <span v-else>
            {{scope.row.id}}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template scope="scope">
          <el-button type="text" v-if="!scope.row.visible" @click="scope.row.visible=true" style="color:blue">编辑</el-button>
          <el-button type="text" v-if="scope.row.visible==true" @click="update(scope.row)" style="color:blue">更新</el-button>
          <el-button type="text" v-if="scope.row.visible==true" @click="scope.row.visible=false" style="color:blue">取消更新</el-button>
          <el-button type="text" @click="del(scope.row.id)" style="color:red">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="添加部门" size="tiny" :visible.sync="addVisible">
      <el-form :model="form" label-width="100px">
        <el-form-item label="部门名称:">
          <el-input style="width:200px" v-model="form.name" placeholder=""></el-input>
          <el-button type="primary" @click="add">添加</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import * as api from "@/api/systemManage";
export default {
  data() {
    return {
      addVisible: false,
      prop: [
        {
          name: "ID",
          label: "#编号"
        },
        {
          name: "name",
          label: "部门名称"
        }
      ],
      List: [],
      form: {
        name: ""
      },
      visible: false
    };
  },
  methods: {
    async add() {
      let { data } = await api.addDepartMent(this.form);
      if (data) {
        this.$message({
          message: "添加成功！",
          type: "success"
        });
      }
      this.addVisible = false;
      this.search();
    },
    async update(row) {
      let {data} = await api.updateDepartMent(row);
      if(data){
        this.$message({
          message: '更新成功！',
          type: 'success'
        });
      }
      row.visible = false;
    },
    async del(ID) {
      this.$confirm("确认要删除此数据吗？", "是否继续", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          let { data } = await api.deleteDept(ID);
          if (data) {
            this.$message({
              message: "删除成功！",
              type: "info"
            });
          }
          this.search();
        })
        .catch(() => {
          this.$message({
            message: "取消删除",
            type: "info"
          });
        });
    },
    async search() {
      let { data } = await api.getDeptInfo();
      data.forEach(x => {
        x.visible = false;
      });
      this.List = data;
    }
  },
  mounted() {
    this.search();
  }
};
</script>


<style scoped>
</style>
